/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.statemachine.editpart;

import java.beans.PropertyChangeListener;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.uml2.uml.Transition;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.connection.NamedGraphEdgeEditPart;
import com.cea.papyrus.core.figures.MiddleEndpointLocator;
import com.cea.papyrus.core.figures.connection.SolidLineFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * This is the controller of the {@link Transition}.
 * 
 * @author David Servat
 * @since 10 apr. 07
 */
public class TransitionEditPart extends NamedGraphEdgeEditPart implements PropertyChangeListener {

	/** The guardAndAll child node. */
	protected TransitionGuardAndAllDisplayEditPart theGuardAndAll;
		
	public TransitionEditPart(GraphEdge graphEdge, ModelManager modelManager){
		super(graphEdge, modelManager);
	}
	
	/**
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	@Override
	protected IFigure createFigure(){
		return new SolidLineFigure();
	}

    /**
     * Creates the edit policies.
     */
    @Override
    protected void createEditPolicies() {
        super.createEditPolicies();
//      Allows the removal of the connection model element
        installEditPolicy(EditPolicy.CONNECTION_ROLE, new GraphEdgeEditPolicy());
    }

	/**
	 * Gets the guard and all display edit part
	 */
	public TransitionGuardAndAllDisplayEditPart getGuardAndAllDisplayEditPart(){
		return theGuardAndAll;
	}
	
	/**
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createChild(java.lang.Object)
	 */
	@Override
	protected EditPart createChild(Object model){
		if (model instanceof GraphNode){
			if (((GraphNode)model) == (GraphNode)getModelChildren().get(0)){
				theNameLabel = new LinkNameDisplayEditPart((GraphNode)getModelChildren().get(0),getUmlElement(),modelManager);
				return theNameLabel;
			}
			if (((GraphNode)model) == (GraphNode)getModelChildren().get(1)){
				theGuardAndAll = new TransitionGuardAndAllDisplayEditPart((GraphNode)getModelChildren().get(1),getUmlElement(),modelManager);
				return theGuardAndAll;
			}
		}
		return super.createChild(model);
	}

	/**
	 * @see com.cea.papyrus.common.editpart.GraphEdgeEditPart#refreshVisuals()
	 */
	@Override
	protected void refreshVisuals() {
		super.refreshVisuals();
		
		Point middleLabel;
		MiddleEndpointLocator lo;
		
		if (theNameLabel != null) {
			theNameLabel.refresh();
			//calculate middle point of the label
			middleLabel =new Point(
					theNameLabel.getGraphNode().getPosition().x-((Label) theNameLabel.getFigure()).getPreferredSize().width/2,
					theNameLabel.getGraphNode().getPosition().y-((Label) theNameLabel.getFigure()).getPreferredSize().height/2
			);
			//calculate middle point of the link
			lo =new MiddleEndpointLocator ((PolylineConnection)this.getFigure(),
					middleLabel);
	
			IFigure f=theNameLabel.getFigure();
			f.setLocation(theNameLabel.getGraphNode().getPosition());//set label a the good position
			if (f.isVisible()){
				this.getContentPane().getLayoutManager().setConstraint(theNameLabel.getFigure(),lo);
			}
		}
		
		if (theGuardAndAll != null) {
			theGuardAndAll.refresh();
			//calculate middle point of the label
			middleLabel = new Point(
					theGuardAndAll.getGraphNode().getPosition().x-((Label) theGuardAndAll.getFigure()).getPreferredSize().width/2,
					theGuardAndAll.getGraphNode().getPosition().y-((Label) theGuardAndAll.getFigure()).getPreferredSize().height/2
			);
			//calculate middle point of the link
			lo = new MiddleEndpointLocator ((PolylineConnection)this.getFigure(),
					middleLabel);
	
			IFigure f=theGuardAndAll.getFigure();
			f.setLocation(theGuardAndAll.getGraphNode().getPosition());//set label a the good position
			if (f.isVisible()){
				this.getContentPane().getLayoutManager().setConstraint(theGuardAndAll.getFigure(),lo);
			}
		}
	}

	@Override
	public Transition getUmlElement(){
		return (Transition)((Uml1SemanticModelBridge)getGraphEdge().getSemanticModel()).getElement();
	}
}